package com.example.administrator.view;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import com.example.administrator.adapter.DividerItemDecoration;
import com.example.administrator.adapter.MsgContentRecyclerViewAdapter;
import com.example.administrator.base.BaseActivity;
import com.example.administrator.bean.MsgContent;
import com.example.administrator.module.UrlDefine;
import com.example.administrator.testapk.R;
import com.example.administrator.utils.NetUtils;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Response;

public class AtyUltraMsg extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener{
    @BindView(R.id.rv_umsg)
    RecyclerView rv_umsg;
    @BindView(R.id.title_umsg)
    TitleBar title_umsg;

    private static final String TAG = "AtyUltraMsg";
    private MsgContentRecyclerViewAdapter msgContentRecyclerViewAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.aty_ultra_msg;
    }


    protected void init() {
        Bundle bundle = new Bundle();
        String s1 = bundle.getString("searchPassword");
        String s2 = bundle.getString("verifyCode");

        Map<String,String> reqbody = new ConcurrentSkipListMap<>();
        reqbody.put("searchPassword", s1);
        reqbody.put("verifyCode", s2);

        NetUtils.getInstance().postDataAsynToNet(UrlDefine.ACTION_URL_SEARCHULTRA,reqbody,new NetUtils.MyNetCall() {
            @Override
            public void success(Call call, Response response) throws IOException {
                final String emsg = response.body().string();

                Gson gson = new Gson();
                final MsgContent msgContent = gson.fromJson(emsg,MsgContent.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(msgContent.getStatus() == 200){
                            final List<MsgContent.DataBean.ListBean> list = msgContent.getData().getList();

                            msgContentRecyclerViewAdapter = new MsgContentRecyclerViewAdapter(AtyUltraMsg.this, (ArrayList<MsgContent.DataBean.ListBean>) list);
                            mLayoutManager = new LinearLayoutManager(AtyUltraMsg.this,
                                    LinearLayoutManager.VERTICAL, false);
                            rv_umsg.setAdapter(msgContentRecyclerViewAdapter);
                            rv_umsg.setLayoutManager(mLayoutManager);
                            rv_umsg.setItemAnimator(new DefaultItemAnimator());//默认动画
                            rv_umsg.setHasFixedSize(true);//效率最高

                            msgContentRecyclerViewAdapter.setOnItemClickListener(new MsgContentRecyclerViewAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(View view, int position) {
                                    Bundle bundle = new Bundle();
                                    bundle.putString("systemName",list.get(position).getSystemName());
                                    bundle.putString("account",list.get(position).getAccount());
                                    bundle.putString("pwd",list.get(position).getPassword());
                                    overlay(AtyMsgContent.class,bundle);
                                }
                            });
                        }
                    }
                });
                Log.e(TAG,emsg);
            }
            @Override
            public void failed(Call call, IOException e) {
                Log.e(TAG,e.getMessage());
            }
        });
    }

    @Override
    protected void initView() {
        title_umsg.setLeftImageResource(R.mipmap.back);
        title_umsg.setTitle("超密信息列表");
        title_umsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_umsg.setLayoutManager(manager);
        rv_umsg.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST));
    }

    @Override
    public void onRefresh() {
        //下拉的时候添加项目到数据源
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }
}
